/*! \file Singleton.inl
 *	\brief
 *
 *	created:	2010/02/14
 *	created:	14:2:2010   12:54
 *	filename: 	c:\src\gag-engine\libs\utils\Singleton.inl
 *	file path:	c:\src\gag-engine\libs\utils
 *	file base:	Singleton
 *	file ext:	inl
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */

namespace tt{
namespace utils{

//////////////////////////////////////////////////////////////////////////
namespace impl{

template<typename Instance>
class SingletonHolder
{
public:
	static ScopedPtr<Instance> s_instance;
};

template<typename Instance>
class SingletonDefault
{
public:
	Instance* Create()
	{
		return new Instance();
	}
};


} //namespace impl

//////////////////////////////////////////////////////////////////////////
template<typename Instance>
Singleton<Instance>::Singleton()
{

}

template<typename Instance>
void Singleton<Instance>::CreateInstance()
{
	impl::SingletonHolder<Instance>::s_instance.reset( impl::SingletonDefault<Instance>().Create() );
}

template<typename Instance>
void Singleton<Instance>::CreateInstance( Instance* instance )
{
	impl::SingletonHolder<Instance>::s_instance.reset(instance);
}

template<typename Instance>
void Singleton<Instance>::DestroyInstance()
{
	impl::SingletonHolder<Instance>::s_instance.reset();
}

template<typename Instance>
Instance& Singleton<Instance>::GetInstance()
{
	return *impl::SingletonHolder<Instance>::s_instance.get();
}

} //namespace utils
} //namespace tt
